

from enum import Enum, unique
from ..errors import OperationError

@unique
class UIKey(Enum):
    def __new__(cls, job_type):
        obj = object.__new__(cls)
        obj.job_type = job_type
        return obj

    def __str__(self) -> str:
        return self.value
    
    TextContact_xPath = "text_contact_xpath"
    TextNewFriend_xPath = "text_new_friend_xpath"
    TextAddNewFriend_xPath = "text_add_new_friend_xpath"
    UIAddImage_RI = "ui_add_new_image_resource_id"
    UISearchFriend_RI = "ui_search_new_friend_resource_id"
    UISearchTextBox_RI = "ui_search_text_box_resource_id"
    TextAddToContact_xPath = "text_add_to_contact_xpath"
    UIAddContactTostLabel_RI = "ui_add_contact_tost_label_resource_id"
    UIAddContactTostButton_RI = "ui_add_contact_tost_button_resource_id"
    TextApplicationAddingFriend_xpath = "text_application_adding_friend_xpath"
    TextSendInAddingFriend_xpath = "text_send_in_adding_friend_xpath"
    #聊天界面
    UIVoiceImageButton_RI = "ui_voice_imagebutton_resource_id"
    UIChattingTextBox_RI = "ui_chatting_textbox_resource_id"
    UIEmojoImageButton_RI = "ui_emojo_imagebutton_resource_id"
    UIAddImageButton_RI = "ui_add_imagebutton_resource_id"
class Adapter(object):
    _VERSIONS = {
        'v7.0.13':{
            UIKey.TextContact_xPath.value: '//*[@text="通讯录"]',
            UIKey.TextNewFriend_xPath.value: '//*[@text="新的朋友"]',
            UIKey.UIAddImage_RI.value: "com.tencent.mm:id/dk0",
            UIKey.TextAddNewFriend_xPath.value: '//*[@text="添加朋友"]',
            UIKey.UISearchFriend_RI.value: "com.tencent.mm:id/f9j",
            UIKey.UISearchTextBox_RI.value: "com.tencent.mm:id/bfl",
            UIKey.TextAddToContact_xPath.value: '//*[@text="添加到通讯录"]',
            UIKey.UIAddContactTostLabel_RI.value: "com.tencent.mm:id/dm4",
            UIKey.UIAddContactTostButton_RI.value: "com.tencent.mm:id/dm3",
            UIKey.TextApplicationAddingFriend_xpath.value: '//*[@text="申请添加朋友"]',
            UIKey.TextSendInAddingFriend_xpath.value: '//*[@text="发送"]',
            UIKey.UIVoiceImageButton_RI.value:"com.tencent.mm:id/am9",
            UIKey.UIChattingTextBox_RI.value:"com.tencent.mm:id/ak7",
            UIKey.UIEmojoImageButton_RI.value:"com.tencent.mm:id/amv",
            UIKey.UIAddImageButton_RI.value: "com.tencent.mm:id/ajp"
        },
        'v7.0.19':{
            UIKey.TextContact_xPath.value: '//*[@text="通讯录"]',
            UIKey.TextNewFriend_xPath.value: '//*[@text="新的朋友"]',
            UIKey.UIAddImage_RI.value: "com.tencent.mm:id/dmw",
            UIKey.TextAddNewFriend_xPath.value: '//*[@text="添加朋友"]',
            UIKey.UISearchFriend_RI.value: "com.tencent.mm:id/f98",
            UIKey.UISearchTextBox_RI.value: "com.tencent.mm:id/bhn",
            UIKey.TextAddToContact_xPath.value: '//*[@text="添加到通讯录"]',
            # TODO 这个需要修改为7.0.19版本的
            UIKey.UIAddContactTostLabel_RI.value: "com.tencent.mm:id/dm4",
            # TODO 这个需要修改为7.0.19版本的
            UIKey.UIAddContactTostButton_RI.value: "com.tencent.mm:id/dm3",
            UIKey.TextApplicationAddingFriend_xpath.value: '//*[@text="申请添加朋友"]',
            UIKey.TextSendInAddingFriend_xpath.value: '//*[@text="发送"]',
            UIKey.UIVoiceImageButton_RI.value:"com.tencent.mm:id/anc",
            UIKey.UIChattingTextBox_RI.value:"com.tencent.mm:id/al_",
            UIKey.UIEmojoImageButton_RI.value:"com.tencent.mm:id/anz",
            UIKey.UIAddImageButton_RI.value: "com.tencent.mm:id/aks"
        },
        'v7.0.20':{
            UIKey.TextContact_xPath.value: '//*[@text="通讯录"]',
            UIKey.TextNewFriend_xPath.value: '//*[@text="新的朋友"]',
            UIKey.UIAddImage_RI.value: "com.tencent.mm:id/dmw",
            UIKey.TextAddNewFriend_xPath.value: '//*[@text="添加朋友"]',
            UIKey.UISearchFriend_RI.value: "com.tencent.mm:id/fcn",
            UIKey.UISearchTextBox_RI.value: "com.tencent.mm:id/bhn",
            UIKey.TextAddToContact_xPath.value: '//*[@text="添加到通讯录"]',
            # TODO 这个需要修改为7.0.20版本的
            UIKey.UIAddContactTostLabel_RI.value: "com.tencent.mm:id/dm4",
            # TODO 这个需要修改为7.0.20版本的
            UIKey.UIAddContactTostButton_RI.value: "com.tencent.mm:id/dm3",
            UIKey.TextApplicationAddingFriend_xpath.value: '//*[@text="申请添加朋友"]',
            UIKey.TextSendInAddingFriend_xpath.value: '//*[@text="发送"]',
            UIKey.UIVoiceImageButton_RI.value:"com.tencent.mm:id/anc",
            UIKey.UIChattingTextBox_RI.value:"com.tencent.mm:id/al_",
            UIKey.UIEmojoImageButton_RI.value:"com.tencent.mm:id/anz",
            UIKey.UIAddImageButton_RI.value: "com.tencent.mm:id/aks"
        }
    }

    def __init__(self, version) -> None:
        self._version = None
        if version in Adapter._VERSIONS.keys():
            self._version = Adapter._VERSIONS[version]

    def check_version(self, version:str) -> bool:
        return version in Adapter._VERSIONS.keys()

    def get(self, key:UIKey) -> str:
        if not self._version is None and key.value in self._version.keys():
            return self._version[key.value]

        raise OperationError("Adapter中key %s 不存在。", key)